package com.msb.advanced.归并;

/**
 * @author whf
 * @date 2025-02-24 09:31
 */
public class Tst {

    public static void main(String[] args) {
        int[] arr = new int[]{1,3,2,3,1};
        System.out.println(process(arr, 0, 4));
    }
    
    public static int reversePairs(int[] arr) {
        if (arr== null || arr.length < 2) {
            return 0;
        }
        return process(arr, 0, arr.length - 1);
    }
    
    public static int process(int[] arr, int start, int end) {
        if (start >= end) {
            return 0;
        }
        int mid = start + (end - start) / 2;
        return process(arr, start, mid) + process(arr, mid + 1, end) + merge(arr, start, mid, end);
    }
    
    public static int merge(int[]arr, int start, int mid, int end) {
        int L1 = start;
        int L2 = mid + 1;
        int count = 0;
        do {
            while(L2 <= end && arr[L1] > 2L * arr[L2]) {
                L2++;
            }
            count += L2 - mid - 1;
        } while(++L1 <= mid);
        int left = mid;
        int right = end;
        int[] temp = new int[end - start + 1];
        int idx = end - start;
        while(left >= start && right >= mid + 1) {
            temp[idx--] = arr[left] > arr[right] ? arr[left--] : arr[right--];
        }
        while(left >= start) {
            temp[idx--] = arr[left--];   
        }
        while(right >= mid + 1) {
            temp[idx--] = arr[right--];   
        }
        for(int num : temp) {
            arr[start++] = num;
        }
        return count;
    }


    // for test
    public static int[] generateRandomArray(int maxSize, int maxValue) {
        int[] arr = new int[(int) ((maxSize + 1) * Math.random())];
        for (int i = 0; i < arr.length; i++) {
            arr[i] = (int) ((maxValue + 1) * Math.random()) - (int) (maxValue * Math.random());
        }
        return arr;
    }

    // for test
    public static int[] copyArray(int[] arr) {
        if (arr == null) {
            return null;
        }
        int[] res = new int[arr.length];
        for (int i = 0; i < arr.length; i++) {
            res[i] = arr[i];
        }
        return res;
    }

    // for test
    public static boolean isEqual(int[] arr1, int[] arr2) {
        if ((arr1 == null && arr2 != null) || (arr1 != null && arr2 == null)) {
            return false;
        }
        if (arr1 == null && arr2 == null) {
            return true;
        }
        if (arr1.length != arr2.length) {
            return false;
        }
        for (int i = 0; i < arr1.length; i++) {
            if (arr1[i] != arr2[i]) {
                return false;
            }
        }
        return true;
    }

    // for test
    public static void printArray(int[] arr) {
        if (arr == null) {
            return;
        }
        for (int i = 0; i < arr.length; i++) {
            System.out.print(arr[i] + " ");
        }
        System.out.println();
    }

    // for test
//    public static void main(String[] args) {
//        int testTime = 500000;
//        int maxSize = 100;
//        int maxValue = 100;
//        System.out.println("测试开始");
//        for (int i = 0; i < testTime; i++) {
//            int[] arr1 = generateRandomArray(maxSize, maxValue);
//            int[] arr2 = copyArray(arr1);
//            if (reverPairNumber(arr1) != comparator(arr2)) {
//                System.out.println("Oops!");
//                printArray(arr1);
//                printArray(arr2);
//                break;
//            }
//        }
//        System.out.println("测试结束");
//    }


    // for test
    public static int comparator(int[] arr) {
        int ans = 0;
        for (int i = 0; i < arr.length; i++) {
            for (int j = i + 1; j < arr.length; j++) {
                if (arr[i] > arr[j]) {
                    ans++;
                }
            }
        }
        return ans;
    }
}
